package config

import (
	"github.com/hashicorp/hcl/v2"
)

type Function struct {
	Name string   `hcl:"name,label" cty:"name"`
	Body hcl.Body `hcl:",remain"`

	WorkingDir *hcl.Attribute
	Command    *hcl.Attribute
	Args       *hcl.Attribute
	Env        map[string]*hcl.Attribute
}

func MergeFunction(a, b []Function) []Function {
	c := make([]Function, 0)
	for _, f := range append(a, b...) {
		if idx := indexOf[Function](func(obj Function) string { return obj.Name }, f.Name, c); idx == -1 {
			c = append(c, f)
		} else {
			c[idx] = f
		}
	}
	return c
}
